package com.mall.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.domain.entity.SysUser;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.DateUtils;
import com.mall.common.utils.StringUtils;
import com.mall.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mall.business.domain.TOrderRefund;
import com.mall.business.service.ITOrderRefundService;
import com.mall.common.core.page.TableDataInfo;

/**
 * 退货售后Controller
 *
 * @author ruoyi
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/business/orderrefund" )
public class TOrderRefundController extends BaseController {
    @Autowired
    private ITOrderRefundService tOrderRefundService;

/**
 * 查询退货售后列表
 */
//@RequiresPermissions("business:orderrefund:list")
@GetMapping("/list" )
    public TableDataInfo list(TOrderRefund tOrderRefund) {
        startPage();
        List<TOrderRefund> list = tOrderRefundService.selectTOrderRefundList(tOrderRefund);
        return getDataTable(list);
    }

    /**
     * 导出退货售后列表
     */
    //@RequiresPermissions("business:orderrefund:export")
    @Log(title = "退货售后" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, TOrderRefund tOrderRefund) {
        List<TOrderRefund> list = tOrderRefundService.selectTOrderRefundList(tOrderRefund);
        ExcelUtil<TOrderRefund> util = new ExcelUtil<TOrderRefund>(TOrderRefund. class);
        util.exportExcel(response, list, "退货售后数据" );
    }

    /**
     * 获取退货售后详细信息
     */
    //@RequiresPermissions("business:orderrefund:query")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(tOrderRefundService.getById(id));
    }

    /**
     * 新增退货售后
     */
    //@RequiresPermissions("business:orderrefund:add")
    @Log(title = "退货售后" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOrderRefund tOrderRefund) {
        return toAjax(tOrderRefundService.save(tOrderRefund));
    }

    /**
     * 修改退货售后
     */
    //@RequiresPermissions("business:orderrefund:edit")
    @Log(title = "退货售后" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrderRefund tOrderRefund) {
        return toAjax(tOrderRefundService.updateById(tOrderRefund));
    }

    /**
     * 删除退货售后
     */
    //@RequiresPermissions("business:orderrefund:remove")
    @Log(title = "退货售后" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tOrderRefundService.removeByIds(Arrays.asList(ids)));
    }

    /**
     */
    @PostMapping("checkXx")
    @Transactional
    public AjaxResult checkXx(@RequestBody TOrderRefund vo)
    {
        String checkStatus = vo.getCheckStatus();
        String checkRemark = vo.getCheckRemark();

        if("3".equals(checkStatus)  && StringUtils.isEmpty(checkRemark)){
            return  AjaxResult.error("请填写审核备注");
        }
        TOrderRefund orderRefund = new TOrderRefund();
        orderRefund.setId(vo.getId());
        orderRefund.setCheckStatus(checkStatus);
        orderRefund.setReturnStatus(checkStatus);
        orderRefund.setCheckDate(DateUtils.getNowDate());
        orderRefund.setCheckRemark(checkRemark);
        tOrderRefundService.updateById(orderRefund);
        return AjaxResult.success();
    }
}
